<template>
  <div class="app-container">
    <indexedDb v-if="false" />
    <el-button type="primary" @click="exportExcel">export excel</el-button>
    <!-- 
      固定el-table 中的thead 为 fixed
      height="100px"
      v-el-height-adaptive-table="{bottomOffset: 30}"
     -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Title">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="Author" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Pageviews" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.pageviews }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="Status" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="Display_time" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.display_time }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import indexedDb from './db/IndexedDB'
import { getList,downLoad } from '@/api/table'
import Blob from './excel/Blob'
import Export2Excel from './excel/Export2Excel.js'
import elHeightAdaptiveTable from '@/directive/el-table'
import waves from '@/directive/waves'

export default {
  directives: { waves, elHeightAdaptiveTable },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  components: {
    indexedDb
  },
  data() {
    return {
      list: null,
      listLoading: true,
      tableData: [
        {'index':'0',"nickName": "沙滩搁浅我们的旧时光", "name": "小明"},
        {'index':'1',"nickName": "女人天生高贵", "name": "小红"},
        {'index':'2',"nickName": "海是彩色的灰尘", "name": "小兰"}
      ]
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(response => {
        console.log(response)
        this.list = response.data.items
        this.listLoading = false
      })
    },
    //导出的方法
    exportExcel() {
      // downLoad().then(res => {
      //   console.log(res)
      // })
      require.ensure([], () => {
        const { export_json_to_excel } = require('./excel/Export2Excel');
        const tHeader = ['序号', '昵称', '姓名'];
        // 上面设置Excel的表格第一行的标题
        const filterVal = ['index', 'nickName', 'name'];
        // 上面的index、nickName、name是tableData里对象的属性
        const list = this.tableData;  //把data里的tableData存到list
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '列表excel');
      })
    },

    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    }
  }
}
</script>
